package com.ruoyi.blog.job;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.cms.domain.Article;
import com.ruoyi.cms.mapper.ArticleMapper;
import com.ruoyi.cms.service.IArticleService;
import com.ruoyi.common.utils.Guid;
import com.ruoyi.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Component("recruitmentJob")
public class RecruitmentJob {

    @Resource
    private ArticleMapper articleMapper;

    public void execute() {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        ThreadContext.bind(manager);


        int recruitmentMaxId = Integer.parseInt(StrUtil.emptyToDefault(RedisUtil.getStringValue("recruitmentJobKey"), "0"));
        HttpRequest request = HttpUtil.createGet("https://zkcms.cnxincai.com/api/content/list?pageSize=50&pageNumber=1")
                .header("Sec-Fetch-Site", "same-site")
                .header("Sec-Fetch-Mode", "cors")
                .header("Sec-Fetch-Dest", "empty")
                .header("Origin", "https://zk.cnxincai.com")
                .header("Referer", "https://zk.cnxincai.com/pages/articleInfo/articleInfo")
                .header("x-api-key", "a20cc8d06799924c33f61e0a10733ffd")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");

        JSONObject jsonObject ;
        try {
            jsonObject = JSONObject.parseObject(request.execute().body());
            if (jsonObject.getInteger("code") == 200) {
                JSONArray list = jsonObject.getJSONObject("data").getJSONArray("list");

                int maxint = 0;
                for (Object o : list) {
                    JSONObject info = (JSONObject) o;
                    Integer id = info.getInteger("id");
                    if (recruitmentMaxId < id) {
                        if (maxint < id) {
                            maxint = id;
                        }
                        //System.out.println(info.toJSONString());
                        Integer provinceId = info.getInteger("provinceId");
                        //3156 陕西
                       // getIntroduction(id);
                        if ( provinceId!=null && 3156 == provinceId) {
                            getIntroduction(id);
                        }
                    }
                }
                RedisUtil.setStringValue("recruitmentJobKey", String.valueOf(maxint));
            }
        } catch (Exception e) {
            log.error("获取招聘信息异常", e);
            throw e;
        }

    }

    private void getIntroduction(Integer id) {
        HttpRequest request = HttpUtil.createGet("https://zkcms.cnxincai.com/api/content/view?id=" + id)
                .header("Sec-Fetch-Site", "same-site")
                .header("Sec-Fetch-Mode", "cors")
                .header("Sec-Fetch-Dest", "empty")
                .header("Origin", "https://zk.cnxincai.com")
                .header("Referer", "https://zk.cnxincai.com/pages/articleInfo/articleInfo")
                .header("x-api-key", "a20cc8d06799924c33f61e0a10733ffd")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");

        try {
            JSONObject  jsonObject = JSONObject.parseObject(request.execute().body());
            if (jsonObject.getInteger("code") == 200) {
                JSONObject data = jsonObject.getJSONObject("data");
                String title = data.getString("title");
                String introduction = data.getString("introduction");
                String source = data.getString("source");
                String tags = data.getString("tags");
                String description = data.getString("description");

                Article article = new Article();
                article.setTitle(title);
                if (articleMapper.selectArticleList(article).size()>0) {
                    return;
                }

                article.setYhid("3");
                article.setArticleRegion("top");
                article.setAuthor("cms");
                article.setTitle(title);
                article.setDescription(description);
                article.setKeywords(tags);
                article.setArticleModel("duoguyu");
                article.setCopyFlag(1);
                article.setCategoryId("8");
                article.setLink(source);
                article.setTags("49,");
                article.setCommentFlag("1");
                article.setAvailable(0);
                article.setCreateTime(new Date());
                article.setUpdateTime(new Date());
                article.setContent(introduction);
                article.setId(Guid.get());

                List<Article> articles = articleMapper.selectArticleList(article);
                int n=articleMapper.insertArticle(article);
                n=articleMapper.insertArticleContent(article);
            }
        } catch (Exception e) {
            log.error("获取招聘信息异常", e);
        }
    }
}
